﻿
using System;
using System.Data;
using System.Collections.Generic;
using System.IO;

namespace LDSP.BRL
{
    public partial class Codification : BYFW.BOL.BaseEntity
    {
        public Codification()
        {
            m_tableName = "t_codification";
            m_hasName = true;
            m_hasComment = true;
            m_tblCacheManager.IsEnable = true;
            m_objCacheManager.Size = 1000;
        }

        protected int m_enterpriseID = 0;
        protected DateTime m_releaseDT = DateTime.Now;
        protected DateTime m_implementDT = DateTime.Now;
        protected bool m_isValid = true;
        protected int m_categoryID = 0;
        protected string m_organization = string.Empty;
        protected string m_referenceNumber = string.Empty;
        protected string m_fileName = string.Empty;
        protected string m_fileExtension = string.Empty;
        protected int m_catalogID = 0;
        protected DocType m_docType = DocType.法律法规;
        protected Enterprise m_enterprise = null;
        protected Category m_category = null;
        protected Catalog m_catalog = null;
        protected DateTime m_uploadDT = DateTime.Parse("1970-01-01");
        protected string m_summary = string.Empty;
        protected string m_keyItems = string.Empty;
        protected string m_action = string.Empty;

        protected string m_comment1 = string.Empty;
        protected string m_comment2 = string.Empty;
        protected string m_comment3 = string.Empty;
        protected string m_comment4 = string.Empty;
        protected string m_comment5 = string.Empty;

        /// <summary>
        /// 企业ID
        /// </summary>
        public int EnterpriseID
        {
            get { return m_enterpriseID; }
            set { m_enterpriseID = value; }
        }
        /// <summary>
        /// 发布时间
        /// </summary>
        public DateTime ReleaseDT
        {
            get { return m_releaseDT; }
            set { m_releaseDT = value; }
        }
        /// <summary>
        /// 实施时间
        /// </summary>
        public DateTime ImplementDT
        {
            get { return m_implementDT; }
            set { m_implementDT = value; }
        }
        /// <summary>
        /// 是否有效
        /// </summary>
        public bool IsValid
        {
            get { return m_isValid; }
            set { m_isValid = value; }
        }
        /// <summary>
        /// 类别ID
        /// </summary>
        public int CategoryID
        {
            get { return m_categoryID; }
            set { m_categoryID = value; }
        }
        /// <summary>
        /// 发布机构
        /// </summary>
        public string Organization
        {
            get { return m_organization; }
            set { m_organization = value; }
        }
        /// <summary>
        /// 发布文号
        /// </summary>
        public string ReferenceNumber
        {
            get { return m_referenceNumber; }
            set { m_referenceNumber = value; }
        }
        /// <summary>
        /// 文件名
        /// </summary>
        public string FileName
        {
            get { return m_fileName; }
        }
        /// <summary>
        /// 文件扩展名
        /// </summary>
        public string FileExtension
        {
            get { return m_fileExtension; }
        }
        /// <summary>
        /// 栏目ID
        /// </summary>
        public int CatalogID
        {
            get { return m_catalogID; }
            set { m_catalogID = value; }
        }
        /// <summary>
        /// 文档类型
        /// </summary>
        public DocType DocType
        {
            get { return m_docType; }
            set { m_docType = value; }
        }
        /// <summary>
        /// 上传时间
        /// </summary>
        public DateTime UploadDT
        {
            get { return m_uploadDT; }
            set { m_uploadDT = value; }
        }
        /// <summary>
        /// 概述
        /// </summary>
        public string Summary
        {
            get { return m_summary; }
            set { m_summary = value; }
        }
        /// <summary>
        /// 关键条款
        /// </summary>
        public string KeyItems
        {
            get { return m_keyItems; }
            set { m_keyItems = value; }
        }
        /// <summary>
        /// 实施措施
        /// </summary>
        public string Action
        {
            get { return m_action; }
            set { m_action = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public string Comment1
        {
            get { return m_comment1; }
            set { m_comment1 = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string Comment2
        {
            get { return m_comment2; }
            set { m_comment2 = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string Comment3
        {
            get { return m_comment3; }
            set { m_comment3 = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string Comment4
        {
            get { return m_comment4; }
            set { m_comment4 = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string Comment5
        {
            get { return m_comment5; }
            set { m_comment5 = value; }
        }

        public Enterprise Enterprise
        {
            get
            {
                if (m_enterprise == null)
                {
                    m_enterprise = new Enterprise();
                    if (m_enterpriseID != 0)
                    {
                        m_enterprise.GetProperties(m_enterpriseID);
                    }
                }
                return m_enterprise;
            }
        }
        public Category Category
        {
            get
            {
                if (m_category == null)
                {
                    m_category = new Category();
                    if (m_categoryID != 0)
                    {
                        m_category.GetProperties(m_categoryID);
                    }
                }
                return m_category;
            }
        }
        public Catalog Catalog
        {
            get
            {
                if (m_catalog == null)
                {
                    m_catalog = new Catalog();
                    if (m_catalogID != 0)
                    {
                        m_catalog.GetProperties(m_catalogID);
                    }
                }
                return m_catalog;
            }
        }

        protected override void FillContainer()
        {
            base.FillContainer();
            m_container.Add("enterpriseID", m_enterpriseID);
            m_container.Add("releaseDT", m_releaseDT);
            m_container.Add("implementDT", m_implementDT);
            m_container.Add("isValid", m_isValid ? 1 : 0);
            m_container.Add("categoryID", m_categoryID);
            m_container.Add("organization", m_organization);
            m_container.Add("referenceNumber", m_referenceNumber);
            m_container.Add("fileName", m_fileName);
            m_container.Add("fileExtension", m_fileExtension);
            m_container.Add("catalogID", m_catalogID);
            m_container.Add("docType", (int)m_docType);
            m_container.Add("uploadDT", m_uploadDT);
            m_container.Add("summary", m_summary);
            m_container.Add("keyItems", m_keyItems);
            m_container.Add("action", m_action);

            m_container.Add("comment1", m_comment1);
            m_container.Add("comment2", m_comment2);
            m_container.Add("comment3", m_comment3);
            m_container.Add("comment4", m_comment4);
            m_container.Add("comment5", m_comment5);
        }

        public override bool GetProperties(System.Data.DataRow record)
        {
            if (!base.GetProperties(record))
            {
                return false;
            }
            m_enterpriseID = this.GetIntValue(record, "enterpriseID");
            m_releaseDT = this.GetDateValue(record, "releaseDT");
            m_implementDT = this.GetDateValue(record, "implementDT");
            m_isValid = this.GetBoolValue(record, "isValid");
            m_categoryID = this.GetIntValue(record, "categoryID");
            m_organization = this.GetStringValue(record, "organization");
            m_referenceNumber = this.GetStringValue(record, "referenceNumber");
            m_fileName = this.GetStringValue(record, "fileName");
            m_fileExtension = this.GetStringValue(record, "fileExtension");
            m_catalogID = this.GetIntValue(record, "catalogID");
            m_docType = (DocType)this.GetIntValue(record, "docType");
            m_uploadDT = this.GetDateValue(record, "uploadDT");
            m_summary = this.GetStringValue(record, "summary");
            m_keyItems = this.GetStringValue(record, "keyItems");
            m_action = this.GetStringValue(record, "action");

            m_comment1 = this.GetStringValue(record, "comment1");
            m_comment2 = this.GetStringValue(record, "comment2");
            m_comment3 = this.GetStringValue(record, "comment3");
            m_comment4 = this.GetStringValue(record, "comment4");
            m_comment5 = this.GetStringValue(record, "comment5");
            return true;
        }

        public List<Codification> GetListByCatalogID(int catalogID)
        {
            return this.GetSpecialList<Codification>(null, "catalogID", catalogID);
        }

        public void DeleteByCatalog(string transID, int catalogID)
        {
            List<Codification> docs = new Codification().GetListByCatalogID(catalogID);
            for (int i = 0; i < docs.Count; i++)
            {
                docs[i].Delete(transID);
            }
        }
        public void DeleteByEnterprise(string transID, int enterpriseID)
        {
            List<Codification> docList = this.GetSpecialList<Codification>(transID, "enterpriseID", enterpriseID);
            for (int i = 0; i < docList.Count; i++)
            {
                docList[i].Delete(transID);
            }
        }

        /// <summary>
        /// 删除Instance，删除文件，删除企业的适用关系，失效关系的源和目标包含它的都删除
        /// </summary>
        public override int Delete(string transID)
        {
            this.DeleteFile();
            //删除失效关系
            new R_DisableDoc().DeleteBySourceAndDestination(transID, m_id);
            //删除适用关系
            new R_EnterpriseCodification().DeleteByCodification(transID, m_id);
            return base.Delete(transID);
        }

        /// <summary>
        /// Delete the file if it exists
        /// </summary>
        private void DeleteFile()
        {
            string fileFullPath = this.GetDownloadFileFullName();
            if (File.Exists(fileFullPath))
            {
                File.Delete(fileFullPath);
            }
        }

        public string GetDownloadFileFullName()
        {
            try
            {
                string dir = System.Web.HttpContext.Current.Request.MapPath("~/Docs");
                return Path.Combine(dir, FileExtension);
            }
            catch { return ""; }
        }

        /// <summary>
        /// 根据filename创建fileextension
        /// </summary>
        public void CreateNewFile(string fileName)
        {
            this.DeleteFile();
            m_fileName = fileName;
            string ext = Path.GetExtension(m_fileName);
            m_fileExtension = Guid.NewGuid().ToString() + ext;
        }

        public DataTable GetRecordsByEnterpriseID(int enterpriseID)
        {
            string cond = string.Format("where {0}={1}", DB.GetCol("enterpriseID"), DB.GetVal(enterpriseID));
            return this.GetRecords(cond);
        }

        /// <summary>
        /// 获取系统管理员所有可操作的文档
        /// </summary>
        /// <param name="systemUserID"></param>
        /// <param name="cond"></param>
        /// <returns></returns>
        public DataTable GetRecordsBySystemUserID(int systemUserID, string cond)
        {
            string sql = string.Format("select {0}.* from {0}, {1} where {0}.catalogID={1}.catalogID and {1}.systemUserID={2} and {3}",
                "t_codification",
                "t_r_systemUser_catalog",
                systemUserID,
                this.DB.GetSimpleCondition(cond));
            return this.DB.Select(sql).Tables[0];
        }
        /// <summary>
        /// 获取指定企业已授权的且未适用的所有文档
        /// </summary>
        /// <param name="enterpriseID"></param>
        /// <param name="cond"></param>
        /// <returns></returns>
        public DataTable GetRecordsByEnterpriseIDForAllFavorite(int enterpriseID, string cond)
        {
            string sql = string.Format(@"select * from 
(
select t_codification.*, t_r_enterprise_codification.id as relationID from t_codification 
inner join t_r_enterprise_catalog 
	on t_codification.catalogID=t_r_enterprise_catalog.catalogID 
	and t_r_enterprise_catalog.enterpriseID={0}
left join t_r_enterprise_codification 
	on t_codification.id=t_r_enterprise_codification.docID 
	and t_r_enterprise_codification.enterpriseID={0}
) t1 where relationID is null and {1}",
                                  enterpriseID,
                                  this.DB.GetSimpleCondition(cond));
            return this.DB.Select(sql).Tables[0];
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="enterpriseID"></param>
        /// <param name="cond"></param>
        /// <returns></returns>
        public DataTable GetRecordsByEnterpriseFavorite(int enterpriseID, string cond)
        {
            string sql = string.Format(@"select * from (
select t_codification.*,
t1.id as ecID, t1.name as ecName, t1.comment as ecComment, 
t1.docID as ecDocID, t1.enterpriseID as ecEnterpriseID,
t1.favoriteDT as ecFavoriteDT, t1.priority as ecPriority,
t1.principal as ecPrincipal,t1.action as ecAction, 
t1.keyItems as ecKeyItems, t1.selfEvaluation as ecSelfEvaluation
from t_codification, t_r_enterprise_codification t1 
where t_codification.id = t1.docID and t1.enterpriseID = {0}) t {1}",
                                  enterpriseID,
                                  cond);
            return this.DB.Select(sql).Tables[0];
        }

        public DataTable GetRecordsUploadDate(DateTime oldest)
        {
            string cond = string.Format("where {0}>='{1}'", DB.GetCol("uploadDT"), oldest);
            return this.GetRecords(cond);
        }

        /// <summary>
        /// 企业用户删除文档时的操作
        /// </summary>
        /// <param name="enterpriseUser"></param>
        public void DeleteForEnterpriseUser(EnterpriseUser enterpriseUser)
        {
            //系统文档
            if (this.EnterpriseID == 0)
            {
                //获取列表目的：防止重复记录的脏数据
                List<R_EnterpriseCodification> list = new R_EnterpriseCodification().GetListByEnterpriseAndCodification(enterpriseUser.EnterpriseID, this.ID);
                foreach (var item in list)
                {
                    item.Delete();
                }
            }
            else
            {
                this.Delete();
            }
        }
    }
}
